import Mock from 'mockjs'
const Random = Mock.Random;
import {
	urlParams2Obj
} from './utils.js'
import qs from 'qs'

// 常量定义=======================
// 产品分类
const categoryList = [{
		categoryId: 1,
		categoryName: "香榧",
	},
	{
		categoryId: 2,
		categoryName: "竹子",
	},

	{
		categoryId: 3,
		categoryName: "山黄精",
	},
]

//产品分布情况（左侧第一行）
//参数名
//year 年份 Long
//categoryId 分类 Long, null则所有分类
//返回数据结构参见 result
Mock.mock(new RegExp('/listByCategory'), 'get', options => {
	const {
		url
	} = options;
	const query = urlParams2Obj(url);
	console.log('接口 /listByCategory 参数', query)

	let listPlant = []
	let listSale = []
	categoryList.forEach(item => {
		listPlant.push({
			...item, //产品分类categoryId categoryName
			acreage: Random.float(100, 500, 1, 1), //种植面积			
		})
		listSale.push({
			...item, //产品分类categoryId categoryName
			count: Random.integer(50, 100), //产量
			amount: Random.float(100, 500, 2, 2), //产值
		})
	})

	if (query.categoryId) {
		listPlant = listPlant.filter(item => item.categoryId == query.categoryId)
		listSale = listSale.filter(item => item.categoryId == query.categoryId)
	}

	const result = {
		code: 200,
		msg: "成功获取",
		listPlant,
		listSale
	}
	return result;
});


/* 种植大户情况 （左侧第二行）
参数
year 年份 Long
返回数据结构参见 result */
Mock.mock(new RegExp('/listByFarmer'), 'get', options => {
	const {
		url
	} = options;
	const query = urlParams2Obj(url);
	console.log('接口 /listByFarmer 参数', query)

	let {
		pageNum,
		pageSize
	} = query //分解参数 pageNum 页号  / pageSize页大小

	let recordTotal = Random.integer(25, 30) //总记录数
	let pageTotal = Math.ceil(recordTotal / pageSize) //总页数

	let summaryDataList = []
	for (let i = 1; i <= pageSize; i++) {
		summaryDataList.push({
			id: pageNum * pageSize + i, //序号
			name: ((pageNum - 1) * pageSize + i) + Random.cname(), //姓名
			partyFlag: Random.boolean(), //是否党员
			acreage: Random.float(10, 50, 1, 1), //种植面积
			count: Random.integer(5, 10), //产量
			amount: Random.float(10, 50, 2, 2), //产值
		});
	}

	const result = {
		code: 200,
		msg: "成功获取",
		data: {
			recordTotal,
			pageTotal,
			list: summaryDataList
		}
	}
	return result;
});

/* 近五年销售情况 （右侧第一行）
参数 
year 年份 Long
categoryId 分类 Long, null则所有分类
返回数据结构参见 result */
Mock.mock(new RegExp('/listBySale'), 'get', options => {
	const {
		url
	} = options;
	const query = urlParams2Obj(url);
	console.log('接口 /listBySale 参数', query)
	let year = parseInt(query.year)	
	
	let listSale = []
	for (let i = 1; i <= 3; i++) {
		for (let y = year - 5 + 1; y <= year; y++) {
			listSale.push({
				year: y , //年份	
				categoryId:i, //产品类
				count: Random.float(5, 10, 2, 2), //产量
				amount: Random.float(10, 50, 2, 2), //产值
			});
		}
	}	
	
	if (query.categoryId) {
		listSale = listSale.filter(item => item.categoryId == query.categoryId)
	}
	
	const result = {
		code: 200,
		msg: "成功获取",		
		data:listSale
	}
	return result;
});

/* 各村种植产值情况 （右侧第二行）
参数
year 年份 Long
//categoryId 分类 Long, null则全部
返回数据结构参见 result */
Mock.mock(new RegExp('/listByVillage'), 'get', options => {
	const {
		url
	} = options;
	const query = urlParams2Obj(url);
	console.log('接口 /listByVillage 参数', query)
	
	let listPlant = []
	let listSale = []
	//模拟20各村庄数据
	for (let i = 1; i <= 17; i++) {		
		let villageId = i  
		let villageName = Random.cword(2, 3) + '村' 
		listPlant.push({
			villageId, //村庄id
			villageName, // 村庄名称	
			acreage: Random.float(10, 50, 2, 2), //种植面积 亩			
		});
		listSale.push({
			villageId, //村庄id
			villageName, // 村庄名称			
			count: Random.integer(5, 10, 2, 2), //产量 吨
			amount: Random.float(10, 50, 2, 2), //产值 万元
		});
	}

	const result = {
		code: 200,
		msg: "成功获取",
		listPlant,
		listSale
	}
	return result;
});


/* 主要农产品分布情况 （中间地图数据）
参数
year 年份 Integer
category_id 产品分类id Long 可选
返回数据结构参见 result */
Mock.mock(new RegExp('/listMapData'), 'get', options => {
	const {
		url
	} = options;
	const query = urlParams2Obj(url);
	console.log('接口 /listMapData 参数', query)

	let listVillage = [] //模拟各村庄数据
	for (let i = 1; i <= 17; i++) {
		listVillage.push({
			id: i, //序号
			name: Random.cword(2, 3) + '村', //	
			acreage: 0.0, //种植面积
			count: 0, //产量
			amount: 0.00, //产值
			locationX: Random.float(2, 80, 2, 2), //地图百分比定位
			locationY: Random.float(2, 80, 2, 2), //地图百分比定位
			altitude: Random.integer(300, 1000), //海拔高度
			introduce: Random.cparagraph(), //简介			
		});
	}

	let listPlant = [] //模拟种植数据
	categoryList.forEach(item => {
		for (let i = 1; i <= 17; i++) {
			let locationX = listVillage[i - 1].locationX + Math.random() * 10
			let locationY = listVillage[i - 1].locationY + Math.random() * 6
			locationX = locationX > 100 ? locationX - Math.random() * 10 * 2 : locationX
			locationY = locationY > 100 ? locationY - Math.random() * 6 * 2 : locationY
			listPlant.push({
				villageId: i, //村ID
				...item, //产品分类	    
				acreage: Random.float(10, 50, 2, 2), //种植面积 亩
				locationX: locationX, //地图百分比定位
				locationY: locationY, //地图百分比定位					
			});
		}
	})
	
	let listSale = [] //模拟销售数据
	categoryList.forEach(item => {
		for (let i = 1; i <= 17; i++) {			
			listSale.push({
				villageId: i, //村ID
				categoryId:item.categoryId,//产品分类	
				categoryName: item.categoryName,
				count: Random.float(10, 50, 2, 2), //产量
				amount: Random.float(10, 50, 2, 2), //产值				
			});
		}
	})

	if (query.categoryId) {
		listPlant = listPlant.filter(item => item.categoryId == query.categoryId)
		listSale  = listSale.filter(item => item.categoryId == query.categoryId)
	}

	//总数		
	let totalAcreage = eval(listPlant.map(item => item.acreage).join('+')).toFixed(2)
	let totalCount = eval(listSale.map(item => item.count).join('+')).toFixed(2) //产量 吨
	let totalAmount = eval(listSale.map(item => item.amount).join('+')).toFixed(2)  //产值 万元

	const result = {
		code: 200,
		msg: "成功获取",
		listVillage,
		listPlant,
		listSale,
		totalAcreage,
		totalCount,
		totalAmount
	}
	return result;
});

/* 认证情况 =============================================
参数 无

返回数据结构参见 result */
Mock.mock(new RegExp('/lsd/listCertified'), 'get', options => {
	const {
		url
	} = options;
	const query = urlParams2Obj(url);
	console.log('接口 /listCertified 参数', query)

	let dataList = []
	for (let i = 1; i <= 100; i++) {
		let typeId = Random.integer(1, 4) //typeId integer 1-绿色基地  2-绿色食品 3-有机食品 4-无公害农产品
		dataList.push({
			typeId,
			subjectName: i + "宁海县" + Random.cword(4, 8) + "合作社", //主体名称
			productName: typeId + "-" + Random.cword(2, 6), //产品名称
			acreage: Random.float(200, 2000, 2, 2), //面积(亩)
			count: Random.float(5, 10, 2, 2), //产量(吨)
		})
	}

	return {
		code: 200,
		msg: "成功获取",
		data: dataList
	}
});
